package com.zwz.thread;

import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.cookie.Cookie;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.Map;

@Slf4j
public class TungeeLogin {

    public TungeeLogin() {
    }

    public static  Map<String, String> getCookies( ) throws IOException {
        String url = "https://user.tungee.com/api/individual-user/login";
        String boundary = "----WebKitFormBoundaryBkf3dWGARK2BBDOC";

        // 构建 multipart/form-data 请求体（根据载荷内容）
        String requestBody = "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"phone\"\r\n\r\n" +
                "15366088401\r\n" +
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"password\"\r\n\r\n" +
                "8b12d498bacddb8551ef51926701aafb\r\n" +
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"remember\"\r\n\r\n" +
                "1\r\n" +
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"code\"\r\n\r\n" +  // 空值
                "\r\n" +
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"area_code\"\r\n\r\n" +
                "86\r\n" +
                "--" + boundary + "--\r\n";  // 结束符

//        try {
        Connection.Response response = Jsoup.connect(url)
                .method(Connection.Method.POST)
                // 设置 Headers（与浏览器完全一致）
                .header("Content-Type", "multipart/form-data; boundary=" + boundary)
                .header("authority", "user.tungee.com")
                .header("path", "/api/individual-user/login")
                .header("scheme", "https")
                .header("accept", "*/*")
                .header("accept-encoding", "gzip, deflate, br")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "no-cache")
                .header("origin", "https://user.tungee.com")
                .header("pragma", "no-cache")
                .header("referer", "https://user.tungee.com/users/sign-in")
                .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "empty")
                .header("sec-fetch-mode", "cors")
                .header("sec-fetch-site", "same-origin")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                .header("x-requested-with", "XMLHttpRequest")
                .header("cookie", "__wpkreporterwid_=bdebfec7-b3b6-4533-a4f5-33ecaecc4b37; __last_enter_version=sales; ...") // 完整 Cookie

                // 设置请求体
                .requestBody(requestBody)
                .ignoreContentType(true) // 允许非 HTML 响应
                .ignoreHttpErrors(true)  // 不抛出 HTTP 错误异常
                .execute();

        // 输出结果
       log.info("状态码: " + response.statusCode());
       log.info("响应体: " + response.body());


        // 获取登录后的 cookies
        Map<String, String> cookies = response.cookies();


        return cookies;


    }




//            // 步骤 3：使用 cookies 访问目标页面
//            String targetUrl = "https://sales.tungee.com/api/search/homepage/enterprises";
//            Document doc = Jsoup.connect(targetUrl)
//                    .cookies(cookies) // 使用登录后的 cookies 维持会话
//                    .get();
//



//            String url2 = "https://sales.tungee.com/api/search/homepage/enterprises";
//            String boundary2 = "----WebKitFormBoundaryW7E7wrDEnLf5MRTW"; // 与 Header 中的 boundary 一致
//
//            // 构建 multipart/form-data 请求体
//            String requestBody2 = "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"start\"\r\n\r\n" +
//                    "0\r\n" +
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"end\"\r\n\r\n" +
//                    "10\r\n" +
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"mode\"\r\n\r\n" +
//                    "0\r\n" +
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"secondaryIndustries\"\r\n\r\n" +
//                    "上市企业\r\n" +
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"filter\"\r\n\r\n" +
//                    "{}\r\n" +  // 空 JSON 对象
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"filter_advance_search_filter_pool\"\r\n\r\n" +
//                    "0\r\n" +
//                    "--" + boundary2 + "\r\n" +
//                    "Content-Disposition: form-data; name=\"need_contact_detail_info\"\r\n\r\n" +
//                    "0\r\n" +
//                    "--" + boundary2 + "--\r\n";  // 结束符
//
//            try {
//
//
//                String timestamp = String.valueOf(System.currentTimeMillis());
////                String dataToSign = timestamp + "|" + "your_api_path";
//                String dataToSign = timestamp + "|" + "https://sales.tungee.com/api/search/homepage/enterprises";
//                String signature = SignatureGenerator.generateHmacSha256(dataToSign, "8b12d498bacddb8551ef51926701aafb");
//
//// 设置请求头
////                post.header("x-tonxis-signature", signature);
////                post.header("x-tonxis-timestamp", timestamp);
////
//                Connection.Response response2 = Jsoup.connect(url2)
//                        .method(Connection.Method.POST)
//                        // 设置请求头（与浏览器完全一致）
//                        .header("Content-Type", "multipart/form-data; boundary=" + boundary)
//                        .header("authority", "sales.tungee.com")
//                        .header("path", "/api/search/homepage/enterprises")
//                        .header("scheme", "https")
//                        .header("accept", "*/*")
//                        .header("accept-encoding", "gzip, deflate, br")
//                        .header("accept-language", "zh-CN,zh;q=0.9")
//                        .header("cache-control", "no-cache")
//                        .header("origin", "https://sales.tungee.com")
//                        .header("pragma", "no-cache")
//                        .header("referer", "https://sales.tungee.com/home?entry=1")
//                        .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
//                        .header("sec-ch-ua-mobile", "?0")
//                        .header("sec-ch-ua-platform", "\"Windows\"")
//                        .header("sec-fetch-dest", "empty")
//                        .header("sec-fetch-mode", "cors")
//                        .header("sec-fetch-site", "same-origin")
//                        .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
//                        .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
//                        .header("x-tonxis-sid", "F0UiGSEUmmeGTqdfaeBdO1RMp4pLNUVBWf68YIF25rVjA0Y=")
////                        .header("x-tonxis-signature", "GrmzRX2nRXNxEhNxEhN5RXMx8C1s84NrMJqVMzjrEh2rMztzRPMKRGMOMXfJM4Qz842uLnhHPDr2.KmdocueEPIxgZe8YE+HiHHShK/+mrQrWfHZxg1BscDmzruG0INNu.I.l9EOIEEYF.7.U.XZKtpBL1lkiqMOG2d05OJUsBquPxt1Kv0Jx4wWGujIh99wV0iom1I6eIpozYKzgLINAlmywC+z5n/kHM7Bey7UNnBwGx56KRbWCvD5iFtTv50aSOCigzOOt.CXgj/0iTk3")
//                        .header("x-tonxis-signature", signature)
//                        // 使用完整 Cookie（从浏览器复制）
//                        .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=1e252b09-1579-4065-baa9-259171f3cd56; __last_enter_version=sales; Hm_lvt_d3ee3af8af62f47558a292277356e90f=1744947754,1745221273; Qs_lvt_309705=1744948492%2C1744956213%2C1744956391%2C1744956490%2C1745221273; Qs_pv_309705=3499943857613400000%2C2401862267627224600%2C2373097984748560400%2C635693979573886800%2C1483374110121918000; _uetsid=00dcdb601e8411f08e8985a5cb4ab811; _uetvid=2a1706601c0711f0be3333b35f4687c7; _co_i=6805a5576eceb9637696f09d; _tx_uid=6805a5576eceb9637696f0a2; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b2ff747e-4b39-42fe-aece-249d6b8a3d3f; _tx_sid=MTc0NTIyMTkyMTE0OTo4YTRiMzU0NTQxNTlmZWJjNjA4MTc2ZTZiNTYzMDM0Ng==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745221661,1745225090,1745227144,1745228061; HMACCOUNT=676A9C43EBB88158; acw_tc=0aef832217452857028752204e00624e2e692c8dfe8fc17f113b6cd69cbb8d; SecurityCenterDuId=ImFBV2xWMjdPdVdOMmx2Q2hGMFNTWTVFPSI.GuiE9w.vhSEmw9ak0ubZHebxVL7ip4syGU; remember_token=6806f3776eceb963be410e06|52088269ca06f794d2154f3a970522b082b227f7115c0a4d95cf619cd85529702f9a4edc687084e831e6f553710a1e4b908116d507db004d79e1fac5243e6dd1; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745286039; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNWE1NTc2ZWNlYjk2Mzc2OTZmMGEyIn0.GuiFHA.3MwUQnb9TpgkNxqEHA0_S_-8Hmw; accountCenterSessionId=.eJwlj81OwzAQhN_FZw4b_6zXPcMFkVSISBW9RGt7rdBGQUpSKop4dwxcRjuj-bSaL3VZZRnestopJMBivEdJEgOaKLYBAVR3av0rtH26dX36fO3P274fp-4FoDs9jk-Hh2t7O2_HvrXtKZn9fZ6Oh2eoYBp5nmWq8FVi9UNZZB3VblsuUt3_3xStZ2QUyCRGlxLAVSHGJpSQMBKAASSnwdbcaiqFIZl6OvE6MKM1OmWPzglUgFOm6DACuyQkQj4HzuAoUY5eNBeMsfZtFmp-980ieVj5Q4btfchR7QpPq3z_AHH_Vto.GuiFHQ.1jecwxX1CVMcKJRZuK4iUayIBPY")
//                        .requestBody(requestBody2)
//                        .ignoreContentType(true)  // 允许处理 JSON 响应
//                        .ignoreHttpErrors(true)   // 不抛出 HTTP 错误异常
//                        .execute();
//
//               log.info("HTTP状态码: " + response2.statusCode());
//               log.info("响应内容: \n" + response2.body());


//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }


}